<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;
use App\Http\Responses\CustomResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class SettingController extends BaseController
{
    /**
     * 列表
     *
     * @return void
     */
    public function index(Request $request)
    {
        $config = config('amis');

        return CustomResponse::success($config);
    }

    /**
     * 保存
     *
     * @return void
     */
    public function store(Request $request)
    {
        $input = $request->input();
        unset($input['global']);

        $configPath = config_path('amis.php');
        $data = [
            '/**',
            ' * 配置文件',
            ' * 此配置文件为自动生成，生成时间 '.date('Y-m-d H:i:s'),
            ' * @author zhx',
            ' */',
            'return '.var_export($input, true).';',
        ];

        $content = implode(PHP_EOL, $data);

        $configPath = config_path('amis.php');
        if (File::put($configPath, '<?php'.PHP_EOL.PHP_EOL.$content)) {
            return CustomResponse::ok();
        } else {
            return CustomResponse::error();
        }
    }
}
